
package com.yuan.view;

import com.yuan.graduate.R;
import com.yuan.prefence.PreferenceKeys;
import com.yuan.unit.PreferenceUtil;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 普通的样式
 * 
 * @author YUAN
 */
public class ListPreference extends LinearLayout implements PreferenceKeys {

    private TextView mTitle = null, mSummary = null;
    private LayoutInflater mInflater = null;
    private Context mContext = null;
    private String key = null;

    public ListPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ListPreference(Context context) {
        this(context, null);
    }

    private void init(Context context) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mInflater.inflate(R.layout.menu_settings_item, this);
        mTitle = (TextView) findViewById(R.id.title);
        mSummary = (TextView) findViewById(R.id.summary);

    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
        String summary = PreferenceUtil.getInstance().getString(key, null);
        if (summary != null && summary.length() >= 1)
        {
            setSummary(summary);
        }
    }

    public void setTitle(String title) {
        if (TextUtils.isEmpty(title) == false) {
            mTitle.setText(title);
        }
    }

    public void setTitle(int title) {
        mTitle.setText(mContext.getResources().getString(title));
    }

    public void setSummary(String summary) {
        if (!TextUtils.isEmpty(summary)) {
            mSummary.setVisibility(View.VISIBLE);
            mSummary.setText(summary);
        } else {
            mSummary.setVisibility(View.INVISIBLE);
        }
    }

    public void setSummaryDrawable(int drawableId) {
        setSummaryDrawable(getResources().getDrawable(drawableId));
    }

    public void setSummaryDrawable(Drawable drawable) {
        mSummary.setCompoundDrawablesWithIntrinsicBounds(drawable,
                null, null, null);
    }

    public void setSummary(int summary) {
        setSummary(mContext.getResources().getString(summary));
    }

    public void onDestroy() {
        // TODO Auto-generated method stub
        mTitle = null;
        mSummary = null;
        mInflater = null;
        mContext = null;
        key = null;

    }

}
